package com.bookhouse.controller;

import com.bookhouse.entity.Press;
import com.bookhouse.service.PressService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by CiusCheung on 16/4/6.
 */

@Controller
@RequestMapping("/press")
public class PressController {

    @Resource(name = "pressService")
    private PressService pressService;

    @RequestMapping(value = "toAddPress")
    public String toAddPress(){
        return "press";
    }

    @RequestMapping(value = "addPress")
    public String addPress(Press press){
        if(press.getId() != null && press.getId()>0)
            pressService.update(press);
        else
            pressService.save(press);
        return "redirect:getAllPress";
    }

    @RequestMapping(value = "getAllPress")
    public String getAllPress(HttpServletRequest request){
        request.setAttribute("list",pressService.findAllList(Press.class));
        return "pressEditList";
    }

    @RequestMapping(value = "getPress",method = RequestMethod.POST)
    public String getPress(String pressId,HttpServletRequest request){
        request.setAttribute("press",pressService.get(Press.class,Integer.parseInt(pressId)));
        return "press";
    }

    @RequestMapping(value = "deletePress",method = RequestMethod.POST)
    public String deletePress(String pressId){
        pressService.delete(Press.class,Integer.parseInt(pressId));
        return "redirect:getAllPress";
    }
}
